package TomcatRelated;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.content.Intent;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.Log;
import androidApplication.javaFiles.MainActivity;


/*
 * This class would be a singelton. any function in the project that want's to send messages to the server should
 * use one of the function of this class
 */
public class TomcatProtocolImp implements TomcatProtocol
{

	//***************************************Class Variables*******************************************//

	private static TomcatProtocol _tomProImp = null;
	private final String tag = new String("TomcatProtocolImp");

	//***************************************Constructors*******************************************//	

	private TomcatProtocolImp()
	{
	}


	public static TomcatProtocol getInstance()
	{
		if(_tomProImp == null)
		{
			_tomProImp = new TomcatProtocolImp();
		}

		return _tomProImp;
	}


	//***************************************Public Methods*******************************************//


	public String register(String android_id, String google_account, String c2dm_register_id) 
	{
		Log.v(tag  , "sending registrationID.......");

		//adding arguments.
		List<NameValuePair> listOfParameters = new ArrayList<NameValuePair>();

		listOfParameters.add(new BasicNameValuePair("android_id", android_id));
		listOfParameters.add(new BasicNameValuePair("google_account", google_account));
		listOfParameters.add(new BasicNameValuePair("c2dm_register_id", c2dm_register_id));

		String strResponse = TomcatClient.getTomcatClient().sendMessage("register", listOfParameters);
		return strResponse;
	}


	public String unregister(String android_id) 
	{
		Log.v(tag  , "sending text message.......");

		//adding arguments.
		List<NameValuePair> listOfParameters = new ArrayList<NameValuePair>();

		listOfParameters.add(new BasicNameValuePair("android_id", android_id));

		String strResponse = TomcatClient.getTomcatClient().sendMessage("unregister", listOfParameters);
		return strResponse;
	}


	public String sendTextMessage(String my_android_id, String to_android_id, String message) 
	{
		Log.v(tag  , "sending text message.......");

		//adding arguments.
		List<NameValuePair> listOfParameters = new ArrayList<NameValuePair>();

		listOfParameters.add(new BasicNameValuePair("my_android_id", my_android_id));
		listOfParameters.add(new BasicNameValuePair("to_android_id", to_android_id));
		listOfParameters.add(new BasicNameValuePair("message", message));
		//listOfParameters.add(new BasicNameValuePair("google_account", google_account));


		String strResponse = TomcatClient.getTomcatClient().sendMessage("sendTextMessage", listOfParameters);
		return strResponse;
	}


	public String recieveTextMessage(String my_android_id, String message_id) 
	{
		Log.v(tag  , "Received text message.......");

		//adding arguments, sending response to sender that we received his message
		List<NameValuePair> listOfParameters = new ArrayList<NameValuePair>();

		listOfParameters.add(new BasicNameValuePair("my_android_id", my_android_id));
		listOfParameters.add(new BasicNameValuePair("message_id", message_id));

		String strResponse = TomcatClient.getTomcatClient().sendMessage("recieveTextMessage", listOfParameters);
		return strResponse;
	}


	//latitude, longitude
	public String updatePosition(String my_android_id, double latitude, double longitude) 
	{
		//Log.v(tag  , "updating Position.......");
		Log.v("Update Position","Sending Position to Server");

		//adding arguments.
		List<NameValuePair> listOfParameters = new ArrayList<NameValuePair>();

		listOfParameters.add(new BasicNameValuePair("my_android_id", my_android_id));
		listOfParameters.add(new BasicNameValuePair("latitude", String.valueOf(latitude)));
		listOfParameters.add(new BasicNameValuePair("longitude", String.valueOf(longitude)));		

		String strResponse = TomcatClient.getTomcatClient().sendMessage("updatePosition", listOfParameters);
		return strResponse;	
	}


	public Vector<UserProfile> getUsersPosition(double x1, double y1, double x2, double y2) {
		// TODO Auto-generated method stub
		//Log.v(tag  , "updating Position.......");
		Log.v("Map Update","Receiving Users Positions from Server");

		//adding arguments.
		List<NameValuePair> listOfParameters = new ArrayList<NameValuePair>();
		listOfParameters.add(new BasicNameValuePair("x1", String.valueOf(x1)));
		listOfParameters.add(new BasicNameValuePair("y1", String.valueOf(y1)));
		listOfParameters.add(new BasicNameValuePair("x2", String.valueOf(x2)));
		listOfParameters.add(new BasicNameValuePair("y2", String.valueOf(y2)));

		InputStream isResponse = TomcatClient.getTomcatClient().getXml("usersPositions", listOfParameters);
		//String str = TomcatClient.getTomcatClient().sendMessage("usersPositions", null); //just for printing the XML
		//System.out.println(str);
		return handleUsersPositionsResponse(isResponse);
	}


	public UserProfile getUserProfile(String deviceId) {
		Log.v("UserDetails","requesting "+deviceId+" details from Server");

		//adding arguments.
		List<NameValuePair> listOfParameters = new ArrayList<NameValuePair>();
		listOfParameters.add(new BasicNameValuePair("deviceId", deviceId));

		InputStream isResponse = TomcatClient.getTomcatClient().getXml("userDetails", listOfParameters);
		//String str = TomcatClient.getTomcatClient().sendMessage("userDetails", null); //just for printing the XML
		//System.out.println(str);
		return handleUsersDetails(isResponse);
	}

	//***************************************Private Methods*******************************************//
	
	
	private UserProfile handleUsersDetails(InputStream isResponse) {
		Log.v("UserDetails","Received: "+isResponse);
		try{
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			MyXMLHandler myXMLHandler = new MyXMLHandler();
			xr.setContentHandler(myXMLHandler);
			xr.parse(new InputSource(isResponse));
			UserProfile u =  myXMLHandler.getUser();;
			System.out.println("User: id= "+u.get_android_id() + " c2dm= "+ u.get_c2dm_register_id() 
					+" google= "+u.get_google_account()+" x= "+ u.getX() + " y= "+u.getY());
			return myXMLHandler.getUser();
		}
		catch (Exception e) {
			Log.v("Map Update","XML Pasing Excpetion. Response: "+isResponse);
			e.printStackTrace();
			return null;
		}
	}

	private Vector<UserProfile> handleUsersPositionsResponse(InputStream isResponse) {

		Log.v("Map Update","Received: "+isResponse);
		try{
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			MyXMLHandler myXMLHandler = new MyXMLHandler();
			xr.setContentHandler(myXMLHandler);
			xr.parse(new InputSource(isResponse));
			//			UserProfile u =  myXMLHandler.getAllUsersProfiles().get(0);
			//			System.out.println("User: id= "+u.get_android_id() + " c2dm= "+ u.get_c2dm_register_id() 
			//					+" google= "+u.get_google_account()+" x= "+ u.getX() + " y= "+u.getY());
			return myXMLHandler.getAllUsersProfiles();
		}
		catch (Exception e) {
			Log.v("Map Update","XML Pasing Excpetion. Response: "+isResponse);
			e.printStackTrace();
			return null;
		}
	}


	private void handleRegisterResponse(String strResponse) 
	{	
	}


	private void handleUnregisterResponse(String strResponse) 
	{
		// TODO Auto-generated method stub
	}


	private void handleUpdatePositionResponse(String strResponse) 
	{
		if (strResponse != null){
			Log.v("Update Position", strResponse);
		}
		else {
			Log.v("Update Position", ""+strResponse);
		}

	}

}
